const express = require("express");
const router = express.Router();
const Response = require("../utils/Response");
const tool = require("../utils/tool");

// 引入mysql连接池
const pool = require("../utils/pool");
// 添加评论
router.post("/comment-add", async (req, res) => {
  let { username, content, avatar, vid } = req.body;
  let date_time = tool.getDate();
  let obj = { username, content, avatar, vid, date_time };
  let sql = "insert into comment set ?";
  await new Promise(resolve => {
    pool.query(sql, [obj], (error, result) => {
      if (error) {
        res.send(Response.error(500, error));
        console.log(error);
        throw error;
      }
      resolve();
    });
  });
  // 获取插入消息的id
  let lastId = await new Promise(resolve => {
    pool.query("select last_insert_id()", (err, result) => {
      if (err) {
        console.log(err);
        return;
      }
      resolve(result[0]["last_insert_id()"]);
    });
  });
  console.log(lastId, "===========");
  res.send({
    code: 200,
    message: "插入成功",
    obj: {
      id: lastId + 999999,
      username,
      content,
      avatar,
      vid,
    },
  });
});

// 收藏
router.post("/collect-add", async (req, res) => {
  let { username, vid } = req.body;
  
   let collect = await new Promise(resolve => {
    let sql = "select * from video  where id =?";
    pool.query(sql, [vid], (error, result) => {
      if (error) {
        res.send(Response.error(500, error));
        throw error;
      }
      resolve(result[0].collect);
    });
  });

  collect ++
  pool.query('update video set collect = ? where id = ? ', [collect,vid],(error,result)=>{
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    // console.log(result);
  })

  let time = tool.getDate();

  let obj = { username, vid, time };
  await new Promise(resolve => {
    let sql2 = "insert into user_collect set ?";
    pool.query(sql2, [obj], (error, result) => {
      if (error) {
        res.send(Response.error(500, error));
        throw error;
      }
      resolve();
    });
  });

  res.send({
    code: 200,
    msg: "收藏成功",
    collect:collect
  });
});

// 删除收藏
router.post("/collect-del", async (req, res) => {
  let { id } = req.body;
  let del=await new Promise(resolve => {
    let sql = "delete from user_collect where vid=?";
    pool.query(sql, [id], (error, result) => {
      if (error) {
        res.send(Response.error(500, error));
        throw error;
      }
      resolve(result);
    });
  });
  console.log(del);
  let collect = await new Promise(resolve=>{
    pool.query('select * from video where id = ?', [id], (err,result)=> {
      resolve(result[0]['collect'])
    })
  })
  collect --
  // console.log('减1',collect)
  pool.query('update video set collect =? where id = ?',[collect, id],(error,result)=> {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send({code:200,msg:'取消成功',collect:collect})
  })
});

// 删除评论
router.post("/comment-del", (req, res) => {
  let { id } = req.body;
  let sql = "delete from comment where id=?";
  pool.query(sql, [id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok());
  });
});

// 修改头像
router.post('/avatar-update',async(req,res)=>{
  let {avatar,username} = req.body
  await new Promise(resolve=>{
    pool.query('update user set avatar = ? where username = ?',[avatar,username],(error,result)=>{
      if (error) {
        res.send(Response.error(500, error));
        throw error;
      }
      resolve()
    })
  })
  
  pool.query('update comment set avatar = ? where username = ?',[avatar,username],(error,result)=>{
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    console.log(result);
  })
  res.send({code:200,avatar:avatar,msg:"头像修改成功"})
})
module.exports = router;
